﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Manager.App_Code;
using System.Linq;
using Manager.ViewModels;
using Manager.Common;
using System.Globalization;

namespace Manager.FormAdd_Edit
{
    public partial class frm_Customer : DevExpress.XtraEditors.XtraForm
    {
        NewPearl_HotelDataContext db = new NewPearl_HotelDataContext();
        KhachHangViewModel vmKhachHang = null;
        List<QuocGia> lstQuocGia = null;
        List<NhomKhach> lstNhomKhach = null;
        List<LoaiKhach> lstLoaiKhach = null;
        string MaKhachHangTiep = "";

        public frm_Customer()
        {
            InitializeComponent();
            lstQuocGia = new List<QuocGia>();
            lstNhomKhach = new List<NhomKhach>();
            lstLoaiKhach = new List<LoaiKhach>();
            lstQuocGia = db.QuocGias.ToList();
            lstNhomKhach = db.NhomKhaches.ToList();
            for (int i = 0; i < 2; i++)
            {
                var loaiKhach = new LoaiKhach();
                if (i == 0)
                {
                    loaiKhach.MaLoaiKhach = true;
                    loaiKhach.TenLoaiKhach = "Người lớn";
                }
                else
                {
                    loaiKhach.MaLoaiKhach = false;
                    loaiKhach.TenLoaiKhach = "Trẻ em";
                }
                lstLoaiKhach.Add(loaiKhach);
            }
        }

        public frm_Customer(KhachHang _khachHang)
        {
            InitializeComponent();
            vmKhachHang = new KhachHangViewModel();
            vmKhachHang.MaKhachHang = _khachHang.MaKH;
            vmKhachHang.TenKhachHang = _khachHang.TenKH;
            vmKhachHang.DiaChi = _khachHang.DiaChi;
            vmKhachHang.DienThoai = _khachHang.DienThoai;
            vmKhachHang.SoFax = _khachHang.SoFax;
            vmKhachHang.SinhNhat = _khachHang.SinhNhat;
            vmKhachHang.strSinhNhat = _khachHang.SinhNhat == null ? "" : _khachHang.SinhNhat.Value.ToString("dd/MM/yyyy");
            vmKhachHang.TreEm = _khachHang.TreEm;
            vmKhachHang.Nu = _khachHang.Nu;
            vmKhachHang.SoTaiKhoan = _khachHang.SoTaiKhoan;
            vmKhachHang.HanTaiKhoan = _khachHang.HanTaiKHoan;
            vmKhachHang.CMND = _khachHang.CMND;
            vmKhachHang.Passport = _khachHang.Passport;
            vmKhachHang.QuocTich = db.QuocGias.Single(i => i.MaQuocGia == _khachHang.QuocTich);
            vmKhachHang.NhomKhach = db.NhomKhaches.Single(i => i.MaNhomKhach == _khachHang.MaNhomKhach);
            vmKhachHang.NgayTao = _khachHang.NgayTao;
            vmKhachHang.GhiChu = _khachHang.GhiChu;

            lstQuocGia = new List<QuocGia>();
            lstNhomKhach = new List<NhomKhach>();
            lstLoaiKhach = new List<LoaiKhach>();
            lstQuocGia = db.QuocGias.ToList();
            lstNhomKhach = db.NhomKhaches.ToList();
            for (int i = 0; i < 2; i++)
            {
                var loaiKhach = new LoaiKhach();
                if (i == 0)
                {
                    loaiKhach.MaLoaiKhach = true;
                    loaiKhach.TenLoaiKhach = "Người lớn";
                }
                else
                {
                    loaiKhach.MaLoaiKhach = false;
                    loaiKhach.TenLoaiKhach = "Trẻ em";
                }
                lstLoaiKhach.Add(loaiKhach);
            }
        }
        
        private void frm_Customer_Load(object sender, EventArgs e)
        {
            try
            {
                cmbQuocTich.DataSource = lstQuocGia;
                cmbQuocTich.DisplayMember = "TenQuocGia";
                cmbQuocTich.ValueMember = "MaQuocGia";

                cmbNhomKhach.DataSource = lstNhomKhach;
                cmbNhomKhach.DisplayMember = "TenNhomKhach";
                cmbNhomKhach.ValueMember = "MaNhomKhach";

                cmbLoaiKhach.DataSource = lstLoaiKhach;
                cmbLoaiKhach.DisplayMember = "TenLoaiKhach";
                cmbLoaiKhach.ValueMember = "MaLoaiKhach";

                cmboxLoaiID.SelectedIndex = 0;

                if (vmKhachHang != null)
                {
                    txtKH.Text = vmKhachHang.MaKhachHang.ToString();
                    txtHoTen.Text = vmKhachHang.TenKhachHang;
                    txtNgaySinh.Text = vmKhachHang.strSinhNhat;
                    txtID.Text = vmKhachHang.Passport;
                    txtDiaChi.Text = vmKhachHang.DiaChi;
                    txtDienThoai.Text = vmKhachHang.DienThoai;
                    txtSoTaiKhoan.Text = vmKhachHang.SoTaiKhoan;
                    dateHanTaiKhoan.Text = vmKhachHang.HanTaiKhoan == null ? "" : vmKhachHang.HanTaiKhoan.Value.ToShortDateString();
                    if (!string.IsNullOrEmpty(vmKhachHang.CMND)) 
                    {
                        txtID.Text = vmKhachHang.CMND;
                        cmboxLoaiID.SelectedIndex = 0;
                    }
                    else if (!string.IsNullOrEmpty(vmKhachHang.Passport))
                    {
                        txtID.Text = vmKhachHang.Passport;
                        cmboxLoaiID.SelectedIndex = 1;
                    }                    
                    memoGhiChu.Text = vmKhachHang.GhiChu;
                    if (vmKhachHang.Nu != null && vmKhachHang.Nu == true)
                    {
                        rabtnNu.Checked = true;
                    }
                    else
                    {
                        rabtnNam.Checked = true;
                    }
                    if (vmKhachHang.TreEm != null)
                    {
                        var loaiKhach = lstLoaiKhach.Single(i => i.MaLoaiKhach == vmKhachHang.TreEm);
                        cmbLoaiKhach.SelectedItem = loaiKhach;
                    }
                    if (vmKhachHang.NhomKhach != null)
                        cmbNhomKhach.SelectedItem = vmKhachHang.NhomKhach;
                    if (vmKhachHang.QuocTich != null)
                        cmbQuocTich.SelectedItem = vmKhachHang.QuocTich;
                }
                else
                {                    
                    var MaxID = db.KhachHangs.Max(i => i.MaKH);
                    MaKhachHangTiep = (MaxID + 1).ToString();
                    txtKH.Text = MaKhachHangTiep;
                }
            }
            catch (Exception ex)
            {
                Log.WriteLog("" + ex.Message + "===>" + ex.StackTrace);
                MessageBox.Show("Có lỗi xảy ra!!!", "Thông báo");
            }
        }

        private int Validate()
        {
            int flag = 0;
            if (txtHoTen.Text == "")
            {
                flag = 1;
                dxErrorProvider1.SetError(txtHoTen, "Vui lòng nhập Tên khách hàng");
            }
            if (txtNgaySinh.Text == "")
            {
                flag = 1;
                dxErrorProvider1.SetError(txtNgaySinh, "Vui lòng nhập Ngày sinh");
            }            
            return flag;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                if (Validate() == 0)
                {
                    dxErrorProvider1.ClearErrors();
                }
                else
                {
                    MessageBox.Show("Có lỗi xảy ra!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (vmKhachHang != null)
                {
                    var khachHang = db.KhachHangs.Single(i => i.MaKH == vmKhachHang.MaKhachHang);
                    khachHang.MaKH = CommonFunction.ParseLong(txtKH.Text);
                    khachHang.TenKH = txtHoTen.Text;
                    khachHang.SinhNhat = DateTime.ParseExact(txtNgaySinh.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);
                    khachHang.QuocTich = cmbQuocTich.SelectedValue.ToString();
                    khachHang.MaNhomKhach = (long)cmbNhomKhach.SelectedValue;
                    if (cmboxLoaiID.Text == "Passport")
                    {
                        khachHang.CMND = "";
                        khachHang.Passport = txtID.Text;
                    }
                    else
                    {
                        khachHang.Passport = "";
                        khachHang.CMND = txtID.Text;
                    }
                    khachHang.DiaChi = txtDiaChi.Text;
                    khachHang.DienThoai = txtDienThoai.Text;
                    khachHang.SoTaiKhoan = txtSoTaiKhoan.Text;
                    if (!string.IsNullOrEmpty(dateHanTaiKhoan.Text))
                        khachHang.HanTaiKHoan = dateHanTaiKhoan.DateTime;
                    khachHang.GhiChu = memoGhiChu.Text;
                    if (rabtnNam.Checked)
                        khachHang.Nu = false;
                    else
                        khachHang.Nu = true;
                    var loaiKhach = cmbLoaiKhach.SelectedItem as LoaiKhach;
                    if (loaiKhach.MaLoaiKhach)
                        khachHang.TreEm = false;
                    else
                        khachHang.TreEm = true;
                }
                else
                {
                    KhachHang khachHang = new KhachHang();
                    khachHang.MaKH = CommonFunction.ParseLong(txtKH.Text);
                    khachHang.TenKH = txtHoTen.Text;
                    khachHang.SinhNhat = DateTime.ParseExact(txtNgaySinh.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);
                    khachHang.QuocTich = cmbQuocTich.SelectedValue.ToString();
                    khachHang.MaNhomKhach = (long)cmbNhomKhach.SelectedValue;
                    if (cmboxLoaiID.Text == "Passport")
                    {
                        khachHang.CMND = "";
                        khachHang.Passport = txtID.Text;
                    }
                    else
                    {
                        khachHang.Passport = "";
                        khachHang.CMND = txtID.Text;
                    }
                    khachHang.DiaChi = txtDiaChi.Text;
                    khachHang.DienThoai = txtDienThoai.Text;
                    khachHang.SoTaiKhoan = txtSoTaiKhoan.Text;
                    if (!string.IsNullOrEmpty(dateHanTaiKhoan.Text))
                        khachHang.HanTaiKHoan = dateHanTaiKhoan.DateTime;
                    khachHang.GhiChu = memoGhiChu.Text;
                    khachHang.NgayTao = DateTime.Now;
                    khachHang.SoFax = "";
                    if (rabtnNam.Checked)
                        khachHang.Nu = false;
                    else
                        khachHang.Nu = true;
                    var loaiKhach = cmbLoaiKhach.SelectedItem as LoaiKhach;
                    if (loaiKhach.MaLoaiKhach)
                        khachHang.TreEm = false;
                    else
                        khachHang.TreEm = true;

                    db.KhachHangs.InsertOnSubmit(khachHang);                    
                }
                db.SubmitChanges();
                MessageBox.Show("Lưu dữ liệu thành công!", "Thông báo");
            }
            catch (Exception ex)
            {
                Log.WriteLog("frm_Customer:btnLuu_Click" + ex.Message + "===>" + ex.StackTrace);
                MessageBox.Show("Thất bại!");
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn có muốn đóng không?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.OK)
                this.Close();
        }

        private void btnLuuVaThoat_Click(object sender, EventArgs e)
        {
            this.btnLuu_Click(null, null);
            this.Close();
        }

        private void txtPassport_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Chỉ được nhập số!", "Thông báo");
            }
        }

        private void txtDienThoai_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Chỉ được nhập số!", "Thông báo");
            }
        }

        private void frm_Customer_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F4)
                this.btnLuuVaThoat_Click(sender, e);
            if (e.KeyCode == Keys.F2)
                this.btnLuu_Click(sender, e);
            if (e.KeyCode == Keys.Escape)
                this.btnThoat_Click(sender, e);
        }

    }

    public class LoaiKhach
    {
        public bool MaLoaiKhach { get; set; }
        public string TenLoaiKhach { get; set; }
    }
}